//
//  CLWelcomeViewController.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/12.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit
import SDWebImage

class CLWelcomeViewController: UIViewController {

    // MARK:  懒加载控件
    private lazy var bgImageView:UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()
    //微博头像
    private lazy var iconImageView :UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //判断头像地址  
        if let imageUrl = CLUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large {
            
            imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named:"avatar_default_big"))
            
        }
        //设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
        
    }()
    //欢迎文字
    private lazy var messageLabel :UILabel = {
       let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.orangeColor()
        //判断登录的名字
        if let name = CLUserAccountViewModel.sharedUserAccount.userAccount?.name {
            label.text = "欢迎回来,\(name)"
        }else
        {
            label.text = "欢迎回来"
        }
        //设置label的透明度
        label.alpha = 0
        return label
    }()
    
    // MARK:  控制器一启动 就看到背景图
    override func loadView() {
        self.view = bgImageView
    }
    // MARK:  viewDidLoad
    override func viewDidLoad() {
        setUpUI()
    }
    //视图一出现就加载动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    // MARK:  setUpUI
    private func setUpUI (){
     
        view.addSubview(iconImageView)
        view.addSubview(messageLabel)
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(200)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(10)
            make.centerX.equalTo(view)
        }
    }
    // MARK:  开启动画
    private func startAnimation(){
        
        self.iconImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options:[], animations: { () -> Void in
            //执行约束动画
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //进入首页
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
        }
    }
    
}
